package com.example.testapp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.util.DisplayMetrics;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;

import com.example.testapp.R;
import com.example.testapp.activity.activity.menu.DateActivity;
import com.example.testapp.activity.activity.menu.TimeActivity;
import com.example.testapp.activity.adapter.MainPagerAdapter;
import com.example.testapp.activity.fragment.FoundFragment;
import com.example.testapp.activity.fragment.LearnFragment;
import com.example.testapp.activity.fragment.MeFragment;
import com.example.testapp.activity.fragment.ReadFragment;
import com.example.testapp.activity.util.DisplayUtil;

public class MainActivity extends AppCompatActivity implements PopupMenu.OnMenuItemClickListener {

    private PopupMenu mPopupMenu = null;
    private TabLayout mTabLayout = null;
    private ViewPager mViewPager = null;
    private MainPagerAdapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DisplayMetrics  metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        float width = metrics.widthPixels * metrics.density;
        float height = metrics.heightPixels * metrics.density;
        DisplayUtil.sp2dp(this, height);

        initView();
    }

    private void initView() {
        mViewPager = (ViewPager)findViewById(R.id.viewPager);
        mTabLayout = (TabLayout)findViewById(R.id.tab_layout);

        mAdapter = new MainPagerAdapter(getSupportFragmentManager(),this);
        mAdapter.addFragment(new FoundFragment().newInstance("Found"), "Found");
        mAdapter.addFragment(new LearnFragment().newInstance("Learn"), "Learn");
        mAdapter.addFragment(new ReadFragment().newInstance("Read"), "Read");
        mAdapter.addFragment(new MeFragment().newInstance("Me"), "Me");

        mViewPager.setAdapter(mAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

        for (int i = 0; i < mTabLayout.getTabCount(); i++){
            TabLayout.Tab tab = mTabLayout.getTabAt(i);
            tab.setCustomView(mAdapter.getTabView(i));
        }

        mViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                Toast.makeText(MainActivity.this, "Selected page position: " + position, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case R.id.action_search:
               // Toast.makeText(MainActivity.this, "sea", Toast.LENGTH_SHORT).show();
                break;
            /*case R.id.action_edit:
                Toast.makeText(MainActivity.this, "edi", Toast.LENGTH_SHORT).show();
                break;
            case R.id.action_share:
                Toast.makeText(MainActivity.this, "sha", Toast.LENGTH_SHORT).show();
                break;*/
            case R.id.action_settings:
                mPopupMenu = new PopupMenu(MainActivity.this, findViewById(R.id.action_settings));
                mPopupMenu.getMenuInflater().inflate(R.menu.menu, mPopupMenu.getMenu());
                mPopupMenu.setOnMenuItemClickListener(this);
                mPopupMenu.show();
                break;
        }
        return true;
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()){
            case R.id.date:
                startActivity(new Intent(MainActivity.this, DateActivity.class));
                break;
            case R.id.time:
                startActivity(new Intent(MainActivity.this, TimeActivity.class));
                break;
        }
        return true;
    }
}
